<?php

namespace app\web\controller;
use app\web\model\UnionStoreModel;
use app\web\model\AgentModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 联盟商家
 */
class UnionStoreController extends StoreBaseController
{
 	protected $model;
 	protected $agent_model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new UnionStoreModel();
        $this->agentmodel = new AgentModel();
		$count = $this->model->where('agent_id',session('storeInfo')['id'])->count('id');
		$this->assign('count',$count);
		$p=Db::name('mall_unionapply')->where('union_id',session('storeInfo')['id'])->where('state','0')->count('id');
    	$this->assign('p',$p);
    }
    /**
     * 联盟商家列表
     * @author [longx] 
     */
    public function index( $page = 1){
    	if($this->model->where('agent_id',session('storeInfo')['id'])->find()){
    		$list = $this->model->where('agent_id',session('storeInfo')['id'])->select();
        	return $this->fetch('index', ['list' => $list,'list1' => 0,'list2' => 0]);
    	}else{
    	$list1 = $this->model->select();
    		foreach($list1 as $k=>$v){
    			$where['a.union_id']=$v['id'];
    			$where['a.agent_id']=session('storeInfo')['id'];
    			
    			$row=Db::name('mall_unionapply')->alias('a')
    			->join('mall_unionstore b','a.union_id=b.id','LEFT')
    			->field('b.*,a.add_time time,a.jia_time,a.state')
    			->where($where)->find();
    			if($row){
    				
    				return $this->fetch('index', ['list1' =>0,'list' => 0,'list2'=>$row]);
    			}
    		}
        return $this->fetch('index', ['list1' => $list1,'list' => 0,'list2' => 0]);
    	}

    }
    //成员列表
  public function member($id){
   $data = $this->model->where('id',$id)->find(); 
        $rules      = explode(',', $data['rules']);  
            foreach( $rules as $k=>$v){
        $list[]  = $this->agentmodel->find($v);
      }
	return $this->fetch('member', ['list' => $list]);
    }
    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add(){
    	$store_id = session('storeInfo')['id'];
    	$u_id = session('storeInfo')['u_id'];
    	//获取商家联盟个数
    	$count = $this->model->where('agent_id',session('storeInfo')['id'])->count('id');
        if(request()->isAjax()){
        	if($count > 0){
        		$this->error('添加失败');
        	}
        	$param=input('post.');
			$param['createtime'] = now_time();
			//新增联盟为审核中1
			$param['status'] = '1';
			$param['agent_id'] = $store_id;
			$param['u_id'] = $u_id;
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('union_store/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
	
    /**
     * [edit 修改]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id){
        if(request()->isAjax()){
            $param = input('post.');  
			$param['audittime'] = now_time();       			
			$param['status'] = '2';//审核通过状态为2
			$param['agent_id'] = session('storeInfo')['id'];//获取商家id
			$param['u_id'] = session('storeInfo')['u_id'];//获取商家用户id
			if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('保存成功', url('union_store/index'));
            } else {
                $this->error('保存失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
   /**
     * 站点信息
     * @author [longx] 
     */
    public function site(){
    	if(request()->isAjax()){
            $param = input('post.');  			       
         	if ($this->model->allowField(true)->save($param,$param['id']) !== false) {
                    $this->success('修改成功', url('UnionStore/site'));
            } else {
                $this->error('修改失败');
            }
        }
		$shareUrl = "http://www.auth.cn/union/agent/index.html";
		$agent['shareUrl'] = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
		$id=session('storeInfo')['id'];
        $this->assign('ad',$this->model->where('agent_id',$id)->find());
		$this->assign('code_url',$agent['shareUrl']);
		$this->assign('url',$shareUrl);
        return $this->fetch();
    }
    
    
    //申请加入联盟
    public function apply($union_id=''){
    	$data['union_id']=$union_id;
    	$data['agent_id']=session('storeInfo')['id'];
    	$data['add_time']=now_time();  
    	if(Db::name('mall_unionapply')->insert($data)){
    		
    		   $this->success('申请中', url('union_store/index'));
    		
    	}
    	
    }
    
   //申请审核
   public function audit(){
    	$data['a.union_id']=session('storeInfo')['id'];
    	$data['a.state']=0;
   		$list=Db::name('mall_unionapply')->alias('a')
   		->join('mall_agent b','a.agent_id=b.id','LEFT')
   		->field('a.add_time time,b.*')
   		->where($data)->select();
   		return $this->fetch('audit',['list'=>$list]);
   } 
   
   //同意申请
   public function audit_ok(){
   			$id=session('storeInfo')['id'];
         	$data = $this->model->where('id',$id)->find();
			$rules      = explode(',', $data['rules']);
			$rules[]=input('agent_id');
			$rules=implode(',', $rules);
  			if($this->model->where('id',$id)->update(['rules'=>$rules])){
  				if(Db::name('mall_unionapply')->where('union_id',$id)->update(['state'=>'1','jia_time'=>now_time()])){
  				$this->success('同意中', url('union_store/audit'));
  				}
  			}
   	
   }
   //退出联盟
   public function quit($id){
   	$data = $this->model->where('id',$id)->find();
	$rules      = explode(',', $data['rules']);
    $pid=session('storeInfo')['id'];
     foreach($rules as $k=>$v){
     	if($v == $pid){
     		unset($rules[$k]);
     	}
     }
   	    $rules=implode(',', $rules);
   	    $this->model->where('id',$id)->update(['rules'=>$rules]);
			if(Db::name('mall_unionapply')->where('agent_id',$pid)->delete()){
  				$this->success('退出中', url('union_store/index'));
  				}

   	    
   }
   
   
}
